//
//  MarketListCell.m
//  AppleMarket
//
//  Created by yangy on 19/10/2018.
//  Copyright © 2018 szgsy. All rights reserved.
//

#import "MarketListCell.h"

// model
#import "MarketObject.h"


@interface MarketListCell()
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) UILabel *zdLabel;
@end

@implementation MarketListCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self initSubViews];
    }
    return self;
}

- (void) initSubViews
{
    [self.contentView addSubview:self.nameLabel];
    [self.contentView addSubview:self.priceLabel];
    [self.contentView addSubview:self.zdLabel];
    [self setLayoutSubViews];
}

- (void) updateWithModel:(MarketObject*) model
{
    NSString *text = [NSString stringWithFormat:@"%@ / %@",model.CurrencyUnit,model.ExchangeCurrencyUnit];
    NSInteger location = [text rangeOfString:@"/"].location;
    NSMutableAttributedString *attrText = [[NSMutableAttributedString alloc] initWithString:text];
    [attrText addAttribute:NSFontAttributeName value:kFont(11) range:NSMakeRange(location, text.length - location)];
    [attrText addAttribute:NSForegroundColorAttributeName value:kColorWithHex(0x80878E) range:NSMakeRange(location, text.length - location)];
    
    _nameLabel.attributedText = attrText;
    _priceLabel.text = [NSString stringWithFormat:@"%.6f",[model.CurrentPrice floatValue]];
    _zdLabel.text = [model.DailyChangePerc floatValue] >= 0 ? [NSString stringWithFormat:@"+%.2f%%",[model.DailyChangePerc floatValue]] : [NSString stringWithFormat:@"%.2f%%",[model.DailyChangePerc floatValue]] ;
    _zdLabel.backgroundColor = [model.DailyChangePerc floatValue] >=0 ? kColorWithHex(0x1DD25D) : kColorWithHex(0xF5174D);
}

- (void) setLayoutSubViews
{
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentView);
        make.left.mas_equalTo(self.contentView).offset(16);
    }];
    [_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        //make.center.mas_equalTo(self.contentView);
        make.centerY.mas_equalTo(self.contentView);
        make.right.mas_equalTo(self.contentView.mas_centerX).offset(40);
    }];
    [_zdLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.contentView).offset(-15);
        make.centerY.mas_equalTo(self.contentView);
        make.size.mas_equalTo(CGSizeMake(85, 32));
    }];
}

- (UILabel *)nameLabel
{
    if(!_nameLabel){
        _nameLabel = [UILabel cm_labelWithText:@"" font:kFontBold(17) textColor:kColorWithHex(0xE304C)];
    }
    return _nameLabel;
}

- (UILabel *)priceLabel
{
    if(!_priceLabel){
        _priceLabel = [UILabel cm_labelWithText:@"" font:kFontBold(17) textColor:kColorWithHex(0x0E304C)];
        _priceLabel.textAlignment = NSTextAlignmentRight;
    }
    return _priceLabel;
}

- (UILabel *)zdLabel
{
    if(!_zdLabel){
        _zdLabel = [UILabel cm_labelWithText:@"" font:kFontBold(16) textColor:[UIColor whiteColor]];
        _zdLabel.textAlignment = NSTextAlignmentCenter;
        _zdLabel.backgroundColor = kColorWithHex(0x1DD25D);
    }
    return _zdLabel;
}

@end
